<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_mc_allocation.class.php 2011-07-07 13:45:46 $
 * @Show Note: 设备调拨单
 */
Class zy_mc_allocation extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_mc_allocation()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_mc_allocation.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_mc_allocation");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_mc_allocation();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "allocation_id";
		$where = 1;

		
			$allocation_no = isset($_GET['allocation_no']) ? trim($_GET['allocation_no']) : '';
			$allocation_from = isset($_GET['allocation_from']) ? trim($_GET['allocation_from']) : '';
			$allocation_to = isset($_GET['allocation_to']) ? trim($_GET['allocation_to']) : '';
			$allocation_time = isset($_GET['allocation_time']) ? trim($_GET['allocation_time']) : '';
			$allocation_time_end = isset($_GET['allocation_time_end']) ? trim($_GET['allocation_time_end']) : '';
			if('' != $allocation_no) {
				$where .= " AND allocation_no LIKE '%{$allocation_no}%' ";
				$this->smarty->assign('allocation_no',$allocation_no);
			}
			if('' != $allocation_from) {
				$where .= " AND allocation_from = '{$allocation_from}' ";
				$this->smarty->assign('allocation_from',$allocation_from);
			}
			if('' != $allocation_to) {
				$where .= " AND allocation_to = '{$allocation_to}' ";
				$this->smarty->assign('allocation_to',$allocation_to);
			}
			if(!empty($allocation_time)) {
				$where .= " AND allocation_time > '".strtotime($allocation_time)."' ";
				$this->smarty->assign('allocation_time',strtotime($allocation_time));
			}
			
			if(!empty($allocation_time_end)) {
				$where .= " AND allocation_time < '".strtotime($allocation_time_end)."' ";
				$this->smarty->assign('allocation_time_end',strtotime($allocation_time_end));
			}
		
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);

		//对应验收记录
		$recordArr = array();
		$db_zal = Factory::getDB('zy_allocation_log');
		foreach ($arr as $key=>$value) {
			$temp_arr =  $db_zal->getRecord($value['allocation_id']);
			if(!empty($temp_arr)){
				$recordArr[$value['allocation_id']] = $temp_arr;
				/*
				foreach($temp_arr as $k => $v) {
					if($v['mc_log_id'] > 0) {
						$mcLogIdArr[] = $v['mc_log_id']; //执行ID
					}
				}
				*/
			}
		}
		//dump($mcLogIdArr);
		//getOne($mc_log_id);
		//dump($recordArr);
		$this->smarty->assign('recordArr',$recordArr);
		
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$this->smarty->assign('all_no',getallno());
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			if(empty($temp['allocation_no'])) {
				alertMsg2('调拨单号不允许为空');
			}
			/*
			if(empty($temp['contract_no'])) {
				alertMsg2('请填写合同号');
			}
			*/
			$insrt = $this->db->insert_zy_mc_allocation($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_mc_allocation');
			if(0 < $insrt) { //成功直接跳转
				redirectPage('index.php?c=zy_mc_allocation');
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_allocation');
			}
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$allocation_id = isset($_REQUEST['allocation_id']) ? intval(trim($_REQUEST['allocation_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($allocation_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_mc_allocation');
			}
			//print_r($result);
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "allocation_id=$allocation_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_mc_allocation($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_mc_allocation');
			if(0 <= $update) { //成功直接跳转
				redirectPage('index.php?c=zy_mc_allocation');
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_allocation');
			}
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$allocation_id = isset($_REQUEST['allocation_id']) ? intval(trim($_REQUEST['allocation_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($allocation_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_mc_allocation');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		/*$where = "allocation_id = {$allocation_id}";
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();*/
		//对应验收记录
		$db_zal = Factory::getDB('zy_allocation_log');
		$recordArr =  $db_zal->getJoinRecord($allocation_id,'zal.allocation_log_id');
		//分页，通过查询所有来去
		$rowNo = count($recordArr);
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		//unset($zy_plan_reply_mc_arr);
		for ($i=$offset;$i<($numrows+$offset);$i++){
			if(isset($recordArr[$i]))
			$page_arr[] = $recordArr[$i];
		}
		$this->smarty->assign('recordArr',$page_arr);
		//print_r($recordArr);

		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$allocation_id = isset($_REQUEST['allocation_id']) ? intval(trim($_REQUEST['allocation_id'])) : 0 ;
		$where = "allocation_id=$allocation_id";
		$del=$this->db->delete_zy_mc_allocation($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		//alertMsg($error_no, 'index.php?c=zy_mc_allocation');
		if(0 < $del) { //成功直接跳转
			redirectPage('index.php?c=zy_mc_allocation');
		} else {
			alertMsg($error_no, 'index.php?c=zy_mc_allocation');
		}
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['allocation_id'] = $_POST['allocation_id'];
			$temp['allocation_no'] = $_POST['allocation_no'];

		} else {
			$temp['allocation_id'] = "";
			$temp['allocation_no'] = $_POST['allocation_no'];

		}
		//补充其他的字段
				$temp['allocation_from'] = $_POST['allocation_from'];
				$temp['allocation_to'] = $_POST['allocation_to'];
				$temp['allocation_time'] = strtotime($_POST['allocation_time']);
				$temp['allocation_info'] = $_POST['allocation_info'];
				$temp['file_path'] = $_POST['file_path'];
				$temp['contract_no'] = $_POST['contract_no'];
				$temp['admin_user_id'] = $_POST['admin_user_id'];
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'设备调拨'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'updateFile' => array('updateFile','文件上传',32,false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}

	/**
	 * (功能描述) 文件上传
	 *
	 * @param 
	 * @return
	 */
	function updateFile() {
		$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
		$allocation_id = isset($_REQUEST['allocation_id']) ? intval(trim($_REQUEST['allocation_id'])) : 0 ;
		$error_no = 0;
		//判断是否存在文件上传
		if (!empty($file_path['name'])) {
			$array = array(
				'source_file_name'=>$file_path['name'],
			);
			$file_name = upload_file($_FILES,'file_path',md5($file_path['name'].time()),$_GET['c']);
			$array['file_path'] = $file_name;
			$where = "allocation_id = '{$allocation_id}' ";
			$update = $this->db->update_zy_mc_allocation($array,$where);
			$error_no = 0 > $update ?  $update :'-1502';
		}
		if ($error_no == '-1502') {
			echo json_encode(array('code'=>1));exit;
		}else{
			echo json_encode(array('code'=>2));exit;
		}
	}

}

?>